#define _CRT_SECURE_NO_WARNINGS
#include"SeqList.h"
void SLInit(SL* ps) {
	assert(ps);
	int* a = (int*)malloc(sizeof(int));
	if (a == NULL) {
		printf("malloc failure\n");
		exit(-1);
	}
	ps->arr = a;
	ps->size = ps->capacity = 0;
}

void SLPopFront(SL* ps) {
	assert(ps && ps->size);
	for (int i = 0; i < ps->size-1; i++) {
		ps->arr[i] = ps->arr[i + 1];
	}
	ps->size--;
}
void SLPopBack(SL* ps) {
	assert(ps && ps->size);
	ps->size--;
}

void SLCheckCapacity(SL* ps) {
	assert(ps);
	if (ps->capacity == ps->size) {
		int x = ps->capacity == 0 ? 4 : ps->capacity * 2;
		int* a = (int*)realloc(ps->arr, x * sizeof(int));
		if (a == NULL) {
			perror("realloc fail!");
			exit(-1);
		}
		ps->arr = a;
		ps->capacity = x;
	}
}

void SLPushBack(SL* ps, int x) {
	assert(ps);
	SLCheckCapacity(ps);
	for (int i = ps->size++; i > 0; i--) {
		ps->arr[i] = ps->arr[i - 1];
	}
	ps->arr[0] = x;
}
void SLPushFront(SL* ps, int x) {
	assert(ps);
	SLCheckCapacity(ps);
	ps->arr[ps->size++] = x;
}
void SLInsert(SL* ps, int pos, int x) {
	assert(ps);
	assert(pos >= 0 && pos <= ps->size);
	SLCheckCapacity(ps);
	for (int i = ps->size++; i > pos; i++) {
		ps->arr[i] = ps->arr[i - 1];
	}
	ps->arr[pos] = x;
}
void SLErase(SL* ps, int pos) {
	assert(ps);
	assert(pos >= 0 && pos < ps->size);
	for (int i = pos; i < ps->size - 1; i++) {
		ps->arr[i] = ps->arr[i + 1];
	}
	ps->size--;
}
void print(SL* ps) {
	assert(ps);
	for (int i = 0; i < ps->size; i++) {
		printf("%d ", ps->arr[i]);
	}
	printf("\n");
}